#include<iostream>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<vector>

typedef void (*handler_t)();

std::vector<handler_t> handlers;

void func1()
{
  printf("hello,我是方法1\n");
}

void func2()
{
  printf("hello,我是方法2\n");
}

void Load()
{
  handlers.push_back(func1);
  handlers.push_back(func2);
}



int main()
{
  pid_t id =fork();
  
  if(id==0)
  {
    while(1)
    {
    printf("我是子进程,pid : ............%d\n",getpid());
    sleep(1);
    }
  }
  else
  {
    int status=0;
    while(1)
    {

        pid_t ret = waitpid(-1,&status,WNOHANG);
  
        if(ret>0)
        {
          printf("等待成功，%d,exit signal: %d ,exit code: %d\n",ret,status&0x7F,(status>>8)&0xFF);
        }
        else if(ret==0)
        {
          //等待成功，但是子进程没有退出
          printf("子进程好了没，奥，还没，那我父进程做点其他事吧\n");
          if(handlers.empty()) Load();
          for(auto f:handlers)
          {
            f();
          }
          sleep(1);
        }
    }

    return 0;
      
  }
}

